pimpas.SOAPRequest = Class.create( );
pimpas.Extends(pimpas.SOAPRequest,
			   {
				   wsdlCache: new Array(),
				   method: null,
				   getWsdl: function(url) {
						for(var i=0;i<this.wsdlCache.length;i++)
							if(this.wsdlCache[i].url == url) return this.wsdlCache[i].xml;
						return null;
				   }
			   }
);
pimpas.Extends(pimpas.SOAPRequest.prototype, pimpas.HTTPRequest.Listener.prototype);
pimpas.Extends(pimpas.SOAPRequest.prototype,
			   {
					httpRequestWSDL: null,
					processParameters: function( ) {
						
						var xml = "";
						var parameters = new Array();
						var el = null;
						while(this.parameters.length > 0) {
							el = this.parameters.shift();
							xml += "<" + el[0] + ">" + el[1].toString().replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;") + "</" + el[0] + ">";
						}
						
						//var pString = (this.formString!=null) ? this.formString +"&" : "";
						
						return xml;
					},
					callBack: function(obj, evt, text, xml) {
						switch(evt) {
							case pimpas.HTTPRequest.events.onError:
								this.status = this.callListeners( evt, text );
							break;
							
							case pimpas.HTTPRequest.events.onAbort:
								this.status = this.callListeners( evt  );
							break;
							
							case pimpas.HTTPRequest.events.onComplete:
								// sucess, now invoke web service
								this.log.info("[SOAP]: Requesting WSDL done, for url: "+this.getUrl());
								this.wsdlCache.put( {url: this.getUrl(), xml: xml} );
								// request again
								this.request( );
							break;
						}
					},
					processHeaders: function( ) {
						
					},
					validate: function( ) {
						// get wsdl
						var wsdl = pimpas.SOAPRequest.getWsdl( this.getUrl() );
						if(wsdl == null) {
							this.log.debug("[SOAP] Can't find wsdl for url: "+this.getUrl( )+". Requesting wsdl...");
							// we need retrieve wsdl
							this.httpRequestWSDL = new pimpas.HTTPRequest( );							
							this.httpRequestWSDL.setUrl( this.getUrl() + "?wsdl" );
							this.httpRequestWSDL.setRequestMethod( pimpas.HTTPRequest.methods.GET );
							this.httpRequestWSDL.addListener( this );
							this.httpRequestWSDL.request( );
							return false;
						}
						
						var namespace = wsdl.getAttributeValue( wsdl, 'targetNamespace' );
						var par = this.processParameters( );
						var sr = 
									"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
									"<soap:Envelope " +
									"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
									"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
									"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
									"<soap:Body>" +
									"<" + this.getMethod( ) + " xmlns=\"" + namespace + "\">" +
									par +
									"</" + this.getMethod( ) + "></soap:Body></soap:Envelope>";
						// hack
						this.processParameters = function( ) { return sr; };
						this.setRequestMethod( pimpas.HTTPRequest.methods.POST );
						var soapaction = ((namespace.lastIndexOf("/") != namespace.length - 1) ? namespace + "/" : namespace) + this.getMethod();
						this.headers.push(["SOAPAction", soapaction]);
						this.headers.push(["Content-Type", "text/xml; charset=utf-8"]);
						
						return true;
					},
			   		toString: function() {
						return "[SOAP HTTPREQUEST]";   
			   		}
		}
);

pimpas.Extends(pimpas.SOAPRequest.prototype, pimpas.HTTPRequest.prototype);
pimpas.Extends(pimpas.SOAPRequest, pimpas.HTTPRequest);